﻿//==================================================================== 
//**   晨曦小竹常用工具集
//====================================================================
//**   Copyright © DawnXZ.com 2015 -- QQ：6808240 -- 请保留此注释
//====================================================================
// 文件名称：ProcedureHelper.cs
// 项目名称：数据库操作实用工具集
// 创建时间：2015-08-01 17:34:36
// 创建人员：宋杰军
// 负 责 人：宋杰军
// 参与人员：宋杰军
// ===================================================================
// 修改日期：
// 修改人员：
// 修改内容：
// ===================================================================
using System;
using System.Data;
using System.Data.SqlClient;

namespace DawnXZ.DBUtility {
	/// <summary>
	/// 存储过程执行处理类
	/// </summary>
	public static class ProcedureHelper {
		/// <summary>
		/// 执行存储过程，返回SqlDataReader ( 注意：调用该方法后，一定要对SqlDataReader进行Close )
		/// </summary>
		/// <param name="connString">数据库连接字符串</param>
		/// <param name="storedProcName">存储过程名</param>
		/// <param name="parameters">存储过程参数</param>
		/// <param name="_timeout">超时时间</param>
		/// <returns>SqlDataReader</returns>
		public static SqlDataReader RunProcedure(string connString, string storedProcName, IDataParameter[] parameters, int _timeout = 30) {
			SqlConnection connection = new SqlConnection(connString);
			SqlDataReader returnReader;
			connection.Open();
			SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
			command.CommandType = CommandType.StoredProcedure;
			command.CommandTimeout = _timeout;
			returnReader = command.ExecuteReader(CommandBehavior.CloseConnection);
			return returnReader;
		}
		/// <summary>
		/// 执行存储过程
		/// </summary>
		/// <param name="connString">数据库连接字符串</param>
		/// <param name="storedProcName">存储过程名</param>
		/// <param name="parameters">存储过程参数</param>
		/// <param name="tableName">DataSet结果中的表名</param>
		/// <returns>DataSet</returns>
		public static DataSet RunProcedure(string connString, string storedProcName, IDataParameter[] parameters, string tableName) {
			using (SqlConnection connection = new SqlConnection(connString)) {
				DataSet dataSet = new DataSet();
				connection.Open();
				SqlDataAdapter sqlDA = new SqlDataAdapter();
				sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
				sqlDA.Fill(dataSet, tableName);
				connection.Close();
				return dataSet;
			}
		}
		/// <summary>
		/// 执行存储过程
		/// </summary>
		/// <param name="connString">数据库连接字符串</param>
		/// <param name="storedProcName">存储过程名</param>
		/// <param name="parameters">存储过程参数</param>
		/// <param name="tableName">DataSet结果中的表名</param>
		/// <param name="Times">超时时间</param>
		/// <returns>DataSet</returns>
		public static DataSet RunProcedure(string connString, string storedProcName, IDataParameter[] parameters, string tableName, int Times) {
			using (SqlConnection connection = new SqlConnection(connString)) {
				DataSet dataSet = new DataSet();
				connection.Open();
				SqlDataAdapter sqlDA = new SqlDataAdapter();
				sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
				sqlDA.SelectCommand.CommandTimeout = Times;
				sqlDA.Fill(dataSet, tableName);
				connection.Close();
				return dataSet;
			}
		}
		/// <summary>
		/// 构建 SqlCommand 对象(用来返回一个结果集，而不是一个整数值)
		/// </summary>
		/// <param name="connection">数据库连接</param>
		/// <param name="storedProcName">存储过程名</param>
		/// <param name="parameters">存储过程参数</param>
		/// <returns>SqlCommand</returns>
		private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters) {
			SqlCommand command = new SqlCommand(storedProcName, connection);
			command.CommandType = CommandType.StoredProcedure;
			foreach (SqlParameter parameter in parameters) {
				if (parameter != null) {
					// 检查未分配值的输出参数,将其分配以DBNull.Value.
					if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
						(parameter.Value == null)) {
						parameter.Value = DBNull.Value;
					}
					command.Parameters.Add(parameter);
				}
			}
			return command;
		}
		/// <summary>
		/// 执行存储过程，返回Output输出参数值        
		/// </summary>
		/// <param name="connString">数据库连接字符串</param>
		/// <param name="storedProcName">存储过程名</param>
		/// <param name="paramenters">存储过程参数</param>
		/// <returns>object</returns>
		public static object RunProcedureAt(string connString, string storedProcName, IDataParameter[] paramenters) {
			using (SqlConnection connection = new SqlConnection(connString)) {
				connection.Open();
				SqlCommand command = BuildQueryCommand(connection, storedProcName, paramenters);
				command.ExecuteNonQuery();
				object obj = command.Parameters["@Output_Value"].Value; //@Output_Value和具体的存储过程参数对应
				if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) {
					return null;
				}
				else {
					return obj;
				}
			}
		}
		/// <summary>
		/// 执行存储过程，返回影响的行数        
		/// </summary>
		/// <param name="connString">数据库连接字符串</param>
		/// <param name="storedProcName">存储过程名</param>
		/// <param name="parameters">存储过程参数</param>
		/// <param name="rowsAffected">影响的行数</param>
		/// <returns></returns>
		public static int RunProcedure(string connString, string storedProcName, IDataParameter[] parameters, out int rowsAffected) {
			using (SqlConnection connection = new SqlConnection(connString)) {
				int result;
				connection.Open();
				SqlCommand command = BuildIntCommand(connection, storedProcName, parameters);
				rowsAffected = command.ExecuteNonQuery();
				result = (int)command.Parameters["ReturnValue"].Value;
				//Connection.Close();
				return result;
			}
		}
		/// <summary>
		/// 创建 SqlCommand 对象实例(用来返回一个整数值)    
		/// </summary>
		/// <param name="connection">数据库连接对象</param>
		/// <param name="storedProcName">存储过程名</param>
		/// <param name="parameters">存储过程参数</param>
		/// <returns>SqlCommand 对象实例</returns>
		private static SqlCommand BuildIntCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters) {
			SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
			command.Parameters.Add(new SqlParameter("ReturnValue",
				SqlDbType.Int, 4, ParameterDirection.ReturnValue,
				false, 0, 0, string.Empty, DataRowVersion.Default, null));
			return command;
		}
	}
}
